import service from '@/axios/config'

const default_headers = {
    /**
     * api请求基础路径
     */
    base_url: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL,
    /**
     * 接口成功返回状态码
     */
    result_code: 200,

    /**
     * 接口请求超时时间
     */
    request_timeout: import.meta.env.VITE_REQUEST_TIMEOUT,

    /**
     * 默认接口请求类型
     * 可选值：application/x-www-form-urlencoded multipart/form-data
     */
    default_headers: 'application/json'
}

const request = (option) => {
    const {url, method, params, data, headersType, responseType} = option
    console.log(headersType)
    return service({
        url: url,
        method,
        params,
        data,
        responseType: responseType,
        headers: {
            'Content-Type': headersType || default_headers.default_headers
        }
    })
}

export default {
    get: async (option) => {
        const res = await request({method: 'GET', ...option})
        return res.data
    },
    post: async (option) => {
        const res = await request({method: 'POST', ...option})
        return res.data
    },
    postOriginal: async (option) => {
        return request({method: 'POST', ...option});
    },
    delete: async (option) => {
        const res = await request({method: 'DELETE', ...option})
        return res.data
    },
    put: async (option) => {
        const res = await request({method: 'PUT', ...option})
        return res.data
    },
    download: async (option) => {
        return request({method: 'GET', responseType: 'blob', ...option});
    },
    upload: async (option) => {
        option.headersType = 'multipart/form-data'
        return request({method: 'POST', ...option});
    }
}
